﻿using System;
using System.Web.UI.WebControls;
using BONSAI.BLL;
using System.Linq;

namespace BONSAI.Admin.UC
{
    public partial class ProductsManager : System.Web.UI.UserControl
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDropLoaiTours();
                BindStart(null);
            }
        }
        void BindDropLoaiTours()
        {
            var list = new ListItem("Tất cả", "0");

            var table = (new TypeProductsBO()).GetAll();
                            

            foreach (var dto in table)
            {
                droptypetours.Items.Add(
                    new ListItem(dto.NameSort, dto.TypeProductsID));
            }
            droptypetours.Items.Insert(0, list);
        }
        public void BindStart(string type)
        {
            //// delete all
            //if (Request.QueryString["zxc"] != null)
            //{
            //    if (Request.QueryString["zxc"].ToString() == "1qwertyuiop")
            //    {
            //        em.Trainee_Delete(0);
            //    }
            //}
            ////=============

            var table = type == null
                            ? (new ProductsBo()).GetAll()
                            : (new ProductsBo()).GetAll().Where(x => x.TypeProductsID == type);
            GridView1.DataSource = table;
            GridView1.DataBind();

        }
        protected void lbcreate_Click(object sender, EventArgs e)
        {
            Response.Redirect("ProductsUpdate.aspx");
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            BindStart(!droptypetours.SelectedValue.Equals("0") ? droptypetours.SelectedValue : null);
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var dataKey = GridView1.DataKeys[e.RowIndex];
            if (dataKey != null)
            {
                string key = dataKey.Value.ToString();
                (new ProductsBo()).DeleteByID(key);
            }
            BindStart(!droptypetours.SelectedValue.Equals("0") ? droptypetours.SelectedValue : null);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            BindStart(!droptypetours.SelectedValue.Equals("0") ? droptypetours.SelectedValue : null);
        }
               
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string task = e.CommandName.ToString();
            string ID = e.CommandArgument.ToString();

            if (string.IsNullOrEmpty(ID))
            {
                return;
            }
            switch (task)
            {
                case "choosenew":
                   // tours.Tours_UpdateActive(true, Convert.ToInt32(ID));
                    BindStart(!droptypetours.SelectedValue.Equals("0") ? droptypetours.SelectedValue : null);
                    WebMBox.Show("tours hiển thị lên trang chủ .");
                    break;
                
            }
        }
    }
}